package com.achievement.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.achievement.domain.dto.AllProjectDTO;
import com.achievement.domain.entity.AllProjectEntity;
import com.achievement.domain.vo.AllProjectVO;
import com.achievement.domain.vo.ProjectProductVO;
import com.achievement.mapper.AllProjectMapper;
import com.achievement.service.IAllProjectService;
import com.achievement.service.IProjectProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class AllProjectServiceImpl extends ServiceImpl<AllProjectMapper, AllProjectEntity> implements IAllProjectService {
    @Autowired
    private IProjectProductService projectProductService;

    @Override
    public List<AllProjectVO> selectByProductId(String productId) {
        List<ProjectProductVO> projectProductVOS = projectProductService.selectByProductId(productId);
        List<String> projectIds = projectProductVOS.stream().map(a->a.getProjectId()).collect(Collectors.toList());
        if(CollUtil.isEmpty(projectIds)){
            return new ArrayList<>();
        }
        List<AllProjectVO> voList = this.baseMapper.selectByProjectIds(projectIds);
        return voList;
    }

    @Override
    public List<AllProjectVO> getProjectByUser(AllProjectDTO dto) {
        List<String> chargerCodes = new ArrayList<>();
        if (Objects.nonNull(dto.getChargerCode())) {
            chargerCodes = Arrays.asList(dto.getChargerCode().split(","));
        }
        String projectStatusId = dto.getProjectStatusId();
        List<AllProjectVO> voList = this.baseMapper.getProjectByUser(chargerCodes, projectStatusId);
        return voList;
    }

}
